home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / 17_bit_pd / 17bitgif / utility / amiga / gd_v1_11.lha / gd1.1.1 / webgif.c < prev   
C/C++ Source or Header  |  1994-12-10  |  4KB  |  167 lines

  1. /* Bring in the gd library functions */
  2. #include "gd.h"
  3.  
  4. /* Bring in standard I/O and string manipulation functions */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. int main(argc, argv) 
  9.     int argc;
  10.     char *argv[];
  11. {
  12.     FILE *in;
  13.     FILE *out;
  14.     /* Declare our image pointer */
  15.     gdImagePtr im = 0;
  16.     int i;
  17.        /* We'll clear 'no' once we know the user has made a
  18.         reasonable request. */
  19.     int no = 1;
  20.     /* We'll set 'write' once we know the user's request
  21.         requires that the image be written back to disk. */
  22.     int write = 0;
  23.     /* C programs always get at least one argument; we want at
  24.         least one more (the image), more in practice. */
  25.     if (argc < 2) {
  26.         no = 1;    
  27.         goto usage;
  28.     }
  29.     /* The last argument should be the image. Open the file. */
  30.     in = fopen(argv[argc-1], "rb");    
  31.     if (!in) {
  32.         fprintf(stderr,
  33.             "Error: can't open file %s.\n", argv[argc-1]);
  34.     }
  35.     /* Now load the image. */    
  36.     im = gdImageCreateFromGif(in);
  37.     fclose(in);
  38.     /* If the load failed, it must not be a GIF file. */
  39.     if (!im) {
  40.         fprintf(stderr,
  41.             "Error: %s is not a valid gif file.\n", argv[1]);
  42.         exit(1);    
  43.     }
  44.     /* Consider each argument in turn. */
  45.     for (i=1; (i < (argc-1)); i++) {
  46.         /* -i turns on and off interlacing. */
  47.         if (!strcmp(argv[i], "-i")) {
  48.             if (i == (argc-2)) {
  49.                 fprintf(stderr, 
  50.                 "Error: -i specified without y or n.\n");
  51.                 no = 1;
  52.                 goto usage;
  53.             }
  54.             if (!strcmp(argv[i+1], "y")) {
  55.                 /* Set interlace. */
  56.                 gdImageInterlace(im, 1);
  57.             } else if (!strcmp(argv[i+1], "n")) {
  58.                 /* Clear interlace. */
  59.                 gdImageInterlace(im, 0);
  60.             } else {
  61.                 fprintf(stderr,
  62.                 "Error: -i specified without y or n.\n");
  63.                 no = 1;
  64.                 goto usage;
  65.             }
  66.             i++;
  67.             no = 0;
  68.             write = 1;
  69.         } else if (!strcmp(argv[i], "-t")) {
  70.             /* Set transparent index (or none). */
  71.             int index;
  72.             if (i == (argc-2)) {
  73.                 fprintf(stderr,
  74.         "Error: -t specified without a color table index.\n");
  75.                 no = 1;
  76.                 goto usage;
  77.             }
  78.             if (!strcmp(argv[i+1], "none")) {
  79.                 /* -1 means not transparent. */
  80.                 gdImageColorTransparent(im, -1);
  81.             } else {
  82.                 /* OK, get an integer and set the index. */
  83.                 index = atoi(argv[i+1]);
  84.                 gdImageColorTransparent(im, index);
  85.             }
  86.             i++;
  87.             write = 1;
  88.             no = 0;
  89.         } else if (!strcmp(argv[i], "-l")) {
  90.             /* List the colors in the color table. */
  91.             int j;
  92.             /* Tabs used below. */
  93.             printf("Index    Red    Green    Blue\n");
  94.             for (j=0; (j < gdImageColorsTotal(im)); j++) {
  95.                 /* Use access macros to learn colors. */
  96.                 printf("%d    %d    %d    %d\n",
  97.                     j, 
  98.                     gdImageRed(im, j),
  99.                     gdImageGreen(im, j),
  100.                     gdImageBlue(im, j));
  101.             }
  102.             no = 0;
  103.         } else if (!strcmp(argv[i], "-d")) {
  104.             /* Output dimensions, etc. */
  105.             int t;
  106.             printf("Width: %d Height: %d Colors: %d\n",
  107.                 gdImageSX(im), gdImageSY(im),
  108.                 gdImageColorsTotal(im));
  109.             t = gdImageGetTransparent(im);
  110.             if (t != (-1)) {
  111.                 printf("Transparent index: %d\n", t);
  112.             } else {
  113.                 /* -1 means the image is not transparent. */
  114.                 printf("Transparent index: none\n");
  115.             }
  116.             if (gdImageGetInterlaced(im)) {
  117.                 printf("Interlaced: yes\n");    
  118.             } else {
  119.                 printf("Interlaced: no\n");    
  120.             }
  121.             no = 0;
  122.         } else {
  123.             fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  124.             break;    
  125.         }
  126.     }
  127. usage:
  128.     if (no) {
  129.         /* If the command failed, output an explanation. */
  130.         fprintf(stderr, 
  131.     "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");
  132.         fprintf(stderr, 
  133.     "Where -i controls interlace (specify y or n for yes or no),\n");
  134.         fprintf(stderr, 
  135.     "-l outputs a table of color indexes, -t sets the specified\n");
  136.         fprintf(stderr, 
  137.     "color index (0-255 or none) to be the transparent color, and\n");
  138.         fprintf(stderr,
  139.     "-d reports the dimensions and other characteristics of the image.\n");
  140.         fprintf(stderr, 
  141.     "Note: you may wish to pipe to \"more\" when using the -l option.\n");
  142.     } 
  143.     if (write) {
  144.         /* Open a temporary file. */
  145.         out = fopen("temp.tmp", "wb");
  146.         if (!out) {
  147.             fprintf(stderr,
  148.                 "Unable to write to temp.tmp -- exiting\n");
  149.             exit(1);
  150.         }
  151.         /* Write the new gif. */
  152.         gdImageGif(im, out);
  153.         fclose(out);
  154.         /* Erase the old gif. */
  155.         unlink(argv[argc-1]);
  156.         /* Rename the new to the old. */
  157.         rename("temp.tmp", argv[argc-1]);
  158.     }
  159.     /* Delete the image from memory. */
  160.     if (im) {
  161.         gdImageDestroy(im);
  162.     }
  163.     /* All's well that ends well. */
  164.     return 0;
  165. }
  166.  
  167.